/**
 * grayrabbit Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package com.grayrabbit.slacker.gengerate;

import java.util.Map;

import com.grayrabbit.slacker.config.GenerateConfig;
import com.grayrabbit.slacker.constant.PropertyConsant;
import com.grayrabbit.slacker.interfaces.CodeGenerate;
import com.grayrabbit.slacker.utils.TemplateProcessUtils;

/**
 * mybatis xml 生成工具类
 * 
 * @author liyd
 * @version $Id: MybatisXmlGenerate.java, v 0.1 2012-5-10 下午3:36:17 liyd Exp $
 */
public class MybatisXmlGenerate extends NormalGenerate implements CodeGenerate {

    /** 
     * @see com.grayrabbit.slacker.interfaces.CodeGenerate#generate(com.grayrabbit.slacker.config.GenerateConfig, java.util.Map)
     */
    public void generate(GenerateConfig generateConfig, Map<String, String> modelMap) {

        super.normalDataInit(generateConfig);

        StringBuilder resultVm = new StringBuilder();

        StringBuilder columns = new StringBuilder();

        StringBuilder properties = new StringBuilder();

        String id = null;
        int count = 0;
        //根据属性生成
        for (String[] column : generateConfig.getColumns()) {

            //属性名称
            String propertyName = TemplateProcessUtils.getPropertyName(column[1]);

            if (count == 0) {
                id = column[1];
                //添加主键属性
                modelMap.put("idProperty", propertyName);
            } else {
                columns.append(column[1]);
                columns.append(",");
                properties.append("#{" + propertyName + "}");
                properties.append(",");
            }
            count++;
            resultVm.append(getMapResult(generateConfig, column[1], id, propertyName));

        }

        String entityClassName = this.getClassName(ENTITY_QUALIFIED_CLASS_NAME);

        modelMap.put("package", this.PACKAGE);

        modelMap.put("entityClass", this.ENTITY_QUALIFIED_CLASS_NAME);

        modelMap.put("mapId", TemplateProcessUtils.convertFirstLower(entityClassName) + "Map");

        modelMap.put("className", this.TARGET_QUALIFIED_CLASS_NAME);

        modelMap.put("tableName", this.TABLE_NAME);

        modelMap.put("columns", columns.substring(0, columns.length() - 1));

        modelMap.put("properties", properties.substring(0, properties.length() - 1));

        modelMap.put("resultMapResult", resultVm.toString());

        modelMap.put("id", id);

    }

    /**
     * 生成Map Result
     * 
     * @param columnName
     * @param id
     * @param propertyName
     * @return
     */
    private String getMapResult(GenerateConfig generateConfig, String columnName, String id,
                                String propertyName) {

        //模板文件
        String resultVm = null;
        //判断是否主键
        if (columnName.equalsIgnoreCase(id)) {
            resultVm = TemplateProcessUtils.getTemplate(generateConfig.getProperties()
                .get(PropertyConsant.TEMPLATE).getChildProperties().get("resultMapIdTemplate")
                .getValue());
        } else {
            resultVm = TemplateProcessUtils.getTemplate(generateConfig.getProperties()
                .get(PropertyConsant.TEMPLATE).getChildProperties().get("resultMapResultTemplate")
                .getValue());
        }

        resultVm = resultVm.replaceAll("\\$\\{propertyName\\}", propertyName);
        resultVm = resultVm.replaceAll("\\$\\{columnName\\}", columnName);

        return resultVm;

    }
}
